package com.qsmx.store.app.main.service.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.qsmx.store.app.R;
import com.qsmx.store.app.main.service.entity.ServiceTitleEntity;
import com.qsmx.store.app.main.service.holder.ServiceLeftHolder;

import java.util.List;

public class ServiceLeftAdapter extends RecyclerView.Adapter<ServiceLeftHolder> {

    private Context mContext;

    private List<ServiceTitleEntity.ServiceTitleList> mData;
    private boolean isShowMax = true;

    //定义接口
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    //声明自定义的监听接口
    private ServiceLeftAdapter.OnItemClickListener monItemClickListener = null;

    //提供set方法
    public void setonItemClickListener(ServiceLeftAdapter.OnItemClickListener listener) {
        this.monItemClickListener = listener;
    }

    public ServiceLeftAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<ServiceTitleEntity.ServiceTitleList> mData) {
        this.mData = mData;
    }

    @NonNull
    @Override
    public ServiceLeftHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ServiceLeftHolder holder = new ServiceLeftHolder(LayoutInflater.from(
                mContext).inflate(R.layout.adapter_service_left, viewGroup,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ServiceLeftHolder serviceLeftHolder, final int position) {

        if (isShowMax) {

            if (mData.get(position).isSelect()) {
                serviceLeftHolder.linContent.setBackgroundColor(mContext.getResources().getColor(R.color.theme_yellow_color));
            } else {
                serviceLeftHolder.linContent.setBackgroundColor(mContext.getResources().getColor(R.color.theme_black));
            }
            serviceLeftHolder.tvTitle.setVisibility(View.VISIBLE);
            serviceLeftHolder.tvTitle.setText(mData.get(position).getTitle());
            Glide.with(mContext).load(mData.get(position).getIcon()).into(serviceLeftHolder.ivIcon);

        } else {
            if (mData.get(position).isSelect()) {
                serviceLeftHolder.linContent.setBackgroundColor(mContext.getResources().getColor(R.color.theme_yellow_color));
            } else {
                serviceLeftHolder.linContent.setBackgroundColor(mContext.getResources().getColor(R.color.theme_black));
            }

            serviceLeftHolder.tvTitle.setVisibility(View.GONE);
            serviceLeftHolder.tvTitle.setText(mData.get(position).getTitle());
            Glide.with(mContext).load(mData.get(position).getIcon()).into(serviceLeftHolder.ivIcon);
        }

        serviceLeftHolder.linContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monItemClickListener.onItemClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mData != null && mData.size() > 0) {
            return mData.size();
        } else {
            return 0;
        }
    }

    public void setIsShowMax(boolean showMax) {
        isShowMax = showMax;
    }

    public boolean getIsShowMax() {
        return isShowMax;
    }
}
